var host = $('.header-logo').attr('src').replace('header-logo.png','');
var ad_360 = [];
function ad_360_click(id){
    var t1 = new Date().getTime();
    for(var i=0;i<ad_360[id].clktk.length;i++){
        var t2 = new Date().getTime();
        mylog(ad_360[id].clktk[i]);
    }
}
function ad_360_callback(data){
    ad_360 = data.ads;
    for(var i=0;i<4;i++){
        var title = data.ads[i].title;
        var img = data.ads[i].img;
        var curl = data.ads[i].curl;
        var desc = data.ads[i].desc;
        //var htmlStr = '<li class="ad_360_item"><a target="_blank" href="'+curl+'" onclick="ad_360_click('+i+');"><img onload="this.style.padding = (160-this.height)/2 +\'px 0px\';" class="cover" src="'+img+'" style="height: inherit;background-color: #ddd;"></a><a target="_blank" href="'+curl+'" onclick="ad_360_click('+i+');"><p class="title">'+title+'</p></a><a target="_blank" href="'+curl+'" onclick="ad_360_click('+i+');"><p class="subtitle">广告</p></a></li>';
        var htmlStr = '<li class="ad_360_item"><div class="cover" style="background-image: url(' + host + '360bg/'+Math.ceil(Math.random()*6)+'.jpg);background-repeat: no-repeat;"><a target="_blank" href="' + curl + '" onclick="ad_360_click(' + i + ');"><img style="padding: 27px 0;" src="' + img + '"></a></div><div class="info"><p class="title"><a target="_blank" href="' + curl + '" onclick="ad_360_click(' + i + ');">' + title +'</a></p><p class="subtitle"><span>广告</span></p><p class="tip"><span>评分:</span><span class="mh-star-line star-3"></span></p></div></li>';
        //var htmlStr2 = '<ul class="view-detail-list detail-list-select"><li class="ad_360_item"><a target="_blank" href="' + curl + '" onclick="ad_360_click(' + i + ');"><div class="cover"><img class="img" src="' + img + '"><img class="tag" src="http://css99tel.cdndm5.com/v201802091550/dm5/images/sd/view-discount-red.png"></div><div class="info"><p class="title">' + title +'</p><p class="subtitle"><span class="icon-coin"></span><span class="b">广告</span></p><p class="tip" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">'+desc+'</p></div></a></li></ul>';
        var htmlStr3 = '<li class="ad_360_item"><div class="mh-item" style="background-image: url(' + host + '360bg/'+Math.ceil(Math.random()*6)+'.jpg);background-size: 167px 220px;background-repeat: no-repeat;"><p class="mh-cover" onclick="ad_360_click('+i+');window.open(\'' + curl + '\');" style="cursor: pointer;background-image: url(' + img + ');background-size: contain;background-repeat: no-repeat;background-position: 0 64px;"></p><div class="mh-item-detali"><h2 class="title" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;"><a target="_blank" href="' + curl + '" onclick="ad_360_click(' + i + ');" style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">' + title + '</a></h2><p class="zl">' + desc + '</p><p class="chapter" style="width: 181px;"><span>广告</span></p></div></div></li>';
        var htmlStr4 = '<li class="ad_360_item"><div class="cover" style="background-image: url(' + host + '360bg/'+Math.ceil(Math.random()*6)+'.jpg);background-repeat: no-repeat;"><a target="_blank" href="http://g.wq96f9.com/w/sys_cs.html?c=1913&uid=521276&" onclick="ad_360_click(' + i + ');"><img src="' + host + '360bg/img.png"></a></div><div class="info"><p class="title"><a target="_blank" href="http://g.wq96f9.com/w/sys_cs.html?c=1913&uid=521276&" onclick="ad_360_click(' + i + ');">缘定三生</a></p><p class="subtitle"><span>广告</span></p><p class="tip"><span>评分:</span><span class="mh-star-line star-3"></span></p></div></li>';
        if(i==0){
            $('.sub-manga .list').eq(0).find('li').eq(1).after(htmlStr);
        }
        else if(i==1){
            $('.sub-manga .list').eq(0).find('li').eq(8).after(htmlStr);
            //$('.sub-manga .list').eq(0).find('li').eq(8).after(htmlStr4);
        }
        else if(i==2){
            $('.sub-manga .list').eq(2).find('li').eq(1).after(htmlStr);
        }
        else if(i==3){
            $('.mh-list li').eq(4).remove();
            $('.mh-list li').eq(2).after(htmlStr3);
        }
        if($(window).scrollTop() > $(".ad_360_item").eq(i).offset().top-$(window).height() && $(window).scrollTop() < $(".ad_360_item").eq(i).offset().top+$(".ad_360_item").eq(i).height()){
            for(var j=0;j<data.ads[i].imptk.length;j++){
                mylog(data.ads[i].imptk[j]);
            }
            data.ads[i].isImp = true;
        }
        else{
            data.ads[i].isImp = false;
        }
    }
    $(window).scroll(function () {
        for(var i=0;i<4;i++){
            if(!data.ads[i].isImp && $(window).scrollTop() > $(".ad_360_item").eq(i).offset().top-$(window).height() && $(window).scrollTop() < $(".ad_360_item").eq(i).offset().top+$(".ad_360_item").eq(i).height()){
                for(var j=0;j<data.ads[i].imptk.length;j++){
                    mylog(data.ads[i].imptk[j]);
                }
                data.ads[i].isImp = true;
            }
        }
    });
}
function mylog(url) {
    var img = new Image(),
        key = 'mediav_sio_log_' + Math.floor(Math.random() * 2147483648).toString(36);
    window[key] = img;
    img.onload = img.onerror = img.onabort = function () {
        img.onload = img.onerror = img.onabort = null;

        window[key] = null;
        img = null;
    };

    img.src = url;
}
var ad_360_uid = new Date().getTime().toString() + Math.floor(Math.random()*1000000).toString();
var ad_360_url = "http://show.g.mediav.com/s?type=1&of=4&newf=1&showid=SGSvsO&uid="+ad_360_uid+"&impct=4&jsonp=ad_360_callback&reqtimes=1";
if(!isVip){
    $.ajax({
        url:ad_360_url,
        dataType:"jsonp",
        jsonp:"_callback"
    });
}